package com.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.Application;
import com.webapp.model.ViewApplicationModel;

/**
 * Servlet implementation class EditAppServlet
 */
public class EditAppServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditAppServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		
		Application app = (Application)session.getAttribute("Application");
		
		String birth = request.getParameter("birthTxt");
		String email = request.getParameter("emailTxt");
		String res = request.getParameter("resTxt");
		String spe = request.getParameter("speTxt");
		String verTxt = request.getParameter("verTxt");
		String quaTxt = request.getParameter("quaTxt");
		String anaTxt = request.getParameter("anaTxt");
		String verPerTxt = request.getParameter("verPerTxt");
		String quaPerTxt = request.getParameter("quaPerTxt");
		String anaPerTxt = request.getParameter("anaPerTxt");
		String greSubTxt = request.getParameter("greSubTxt");
		String greScrTxt = request.getParameter("greScrTxt");
		String tstTxt = request.getParameter("tstTxt");
		String ieltTxt = request.getParameter("ieltTxt");
		String resTxt = request.getParameter("resTxt");
		String reTopTxt = request.getParameter("resTopTxt");
		String undTxt = request.getParameter("undTxt");
		String gradTxt = request.getParameter("gradTxt");
		String gradGPA = request.getParameter("gradGPATxt");
		String gradGPA2 = request.getParameter("gradGPA2Txt");
		String uRank = request.getParameter("uRankTxt");
		String progTTxt = request.getParameter("progTTxt");
		String progL = request.getParameter("progLTxt");
		String progS = request.getParameter("progSTxt");
		String progO = request.getParameter("progOtxt");
		
		/*
		app.setBirthDay(Integer.parseInt((String) birth.subSequence(0,2)));
		app.setBirthMonth(Integer.parseInt((String) birth.subSequence(2,5)));
		app.setBirthYear(Integer.parseInt((String) birth.subSequence(5,9)));
		*/
		
		app.setEmail(email);
		
		app.setpPermanentResident(res);
		
		app.setSpecialization(spe);
		
		app.setGreV(Integer.parseInt((String) verTxt));
		app.setGreQ(Integer.parseInt((String) quaTxt));
		app.setGreA(Integer.parseInt((String) anaTxt));
		app.setGreVPctile(Integer.parseInt((String) verPerTxt));
		app.setGreQPctile(Integer.parseInt((String) quaPerTxt));
		app.setGreAPctile(Integer.parseInt((String) anaPerTxt));
		app.setGreSubjName(greSubTxt);
		app.setGreSubjScore(Integer.parseInt(greScrTxt));
		app.setIelts(Integer.parseInt(ieltTxt)); 
		app.setResearchArea(resTxt); 
		app.setResearchTopic(reTopTxt);
		app.setUgInst(undTxt);
		app.setGradInst(gradTxt);
		app.setGradGpa1(Double.parseDouble(gradGPA));
		app.setGradGpa2(Double.parseDouble(gradGPA2)); 
		app.setUgRank(Integer.parseInt(uRank));
		app.setTheorySbuEquiv(progTTxt);
		app.setProgLangSbuEquiv(progL);
		app.setAlgorithmSbuEquiv(progS);
		app.setOsSbuEquiv(progO);
		
		try {
			ViewApplicationModel vam = new ViewApplicationModel();
			vam.editApplication(app);
			session.setAttribute("Application", null); // get rid of app stored in session
			response.sendRedirect("viewApps.jsp?edit=success");
		}catch(Exception E){
			List<String> errorList = new ArrayList();
			errorList.add("Database Connection Error");
			errorList.add("Application Model");
			session.putValue("ErrorObject",errorList);
			response.sendRedirect("error.jsp");
		}
	}
}
